﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using fanfu.Common;
using fanfu.Model;
using System.Data.SqlClient;

namespace fanfu.Business
{
    public class ChannelRule
    {
        //----------后台调用------------

        #region 获取所有频道信息
        /// <summary>
        /// 获取所有频道信息
        /// </summary>
        public DataTable GetAllChannelInfo()
        {
            string sqlString = "SELECT * FROM Channel ORDER BY ParentId ASC, Ord ASC";
            DataTable dt = SqlHelper.GetDataTable(sqlString);
            return dt;
        }
        #endregion

        #region 获取指定ID的频道信息
        /// <summary>
        /// 获取指定ID的频道信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ChannelModel GetChannelByID(string ID)
        {
            string sqlString = "SELECT TOP 1 * FROM Channel WHERE ID=@ID";
            SqlParameter para = new SqlParameter("@ID", ID);
            DataTable dt = SqlHelper.GetDataTable(sqlString, para);
            if (dt.Rows.Count == 1)
                return new ChannelModel(dt.Rows[0]);
            else
                return null;
        }
        #endregion

        #region 删除指定ID的频道信息
        /// <summary>
        /// 删除指定ID的频道信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int DeleteChannel(string ID)
        {
            string sqlString = string.Format("UPDATE Channel SET Flag={0} WHERE ID={1}", (int)ChannelFlag.delete, ID);
            return SqlHelper.ExecuteCommand(sqlString);
        }
        #endregion

        //----------前台调用------------

        #region 获取所有顶级频道,按照排序字段排倒序
        /// <summary>
        /// 获取所有正常状态的顶级频道,按照排序字段排倒序
        /// </summary>
        /// <returns></returns>
        public DataTable GetTopChannel()
        {
            string sqlString = "SELECT * FROM Channel WHERE ParentId=0 AND Flag="+(int)ChannelFlag.normal+" ORDER BY Ord ASC";
            DataTable dt = SqlHelper.GetDataTable(sqlString);
            return dt;
        }
        #endregion
    }
}
